package com.yijiupi.plugin.test.facet;


import com.intellij.facet.ui.FacetEditorContext;
import com.intellij.facet.ui.FacetEditorTab;
import com.intellij.facet.ui.FacetValidatorsManager;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.util.text.StringUtil;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.awt.*;

/**
 * Provides the JPanel to be displayed in the facet UI.
 * Manages validation and modification of the {@link DemoFacet} state.
 * Facet设置界面
 */
public class DemoFacetEditorTab extends FacetEditorTab {
	private static final String FACET_PANEL_PROMPT = "设置服务器地址: ";

	private final DemoFacetState mySettings;
	private final JTextField myPath;

	/**
	 * Only org.intellij.sdk.facet.DemoFacetState is captured so it can be updated per user changes in the EditorTab.
	 *
	 * @param state     {@link DemoFacetState} object persisting {@link DemoFacet} state.
	 * @param context   Facet editor context, can be used to get e.g. the current project module.
	 * @param validator Facet validator manager, can be used to get and apply a custom validator for this facet.
	 */
	public DemoFacetEditorTab(@NotNull DemoFacetState state, @NotNull FacetEditorContext context,
	                          @NotNull FacetValidatorsManager validator) {
		mySettings = state;
		myPath = new JTextField(state.getDemoFacetState());
		myPath.setText("http://www.baidu.com");
	}

	/**
	 * Provides the {@link JPanel} displayed in the Preferences | Facet UI
	 *
	 * @return {@link JPanel} to be displayed in the {@link DemoFacetEditorTab}.
	 */
	@NotNull
	@Override
	public JComponent createComponent() {
		JPanel top = new JPanel(new BorderLayout());
		top.add(new JLabel(FACET_PANEL_PROMPT), BorderLayout.WEST);
		top.add(myPath);
		JPanel facetPanel = new JPanel(new BorderLayout());
		facetPanel.add(top, BorderLayout.NORTH);
		return facetPanel;
	}

	/**
	 * @return the name of this facet for display in this editor tab.
	 */
	@Override
	@Nls(capitalization = Nls.Capitalization.Title)
	public String getDisplayName() {
		return DemoFacetType.FACET_NAME;
	}

	/**
	 * Determines if the facet state entered in the UI differs from the currently stored state.
	 * Called when user changes text in {@link #myPath}.
	 *
	 * @return {@code true} if the state returned from the panel's UI differs from the stored facet state.
	 */
	@Override
	public boolean isModified() {
		return !StringUtil.equals(mySettings.getDemoFacetState(), myPath.getText().trim());
	}

	/**
	 * Stores new facet state (text) entered by the user.
	 * Called when {@link #isModified()} returns true.
	 *
	 * @throws ConfigurationException if anything generates an exception.
	 */
	@Override
	public void apply() throws ConfigurationException {
		// Not much to go wrong here, but fulfill the contract
		try {
			String newTextContent = myPath.getText();
			System.out.println("设置地址:"+newTextContent);
			mySettings.setDemoFacetState(newTextContent);
		} catch(Exception e) {
			throw new ConfigurationException(e.toString());
		}
	}

	/**
	 * Copies current {@link DemoFacetState} into the {@link #myPath} UI element.
	 * This method is called each time this editor tab is needed for display.
	 */
	@Override
	public void reset() {
		myPath.setText(mySettings.getDemoFacetState());
	}

}
